#!/usr/bin/python
# -*- coding: utf-8 -*-

from multiprocessing import Pool
from downloader import Downloader


def _download_process(url, file_path, file_name, session, forced, thread_num):
    downloader = Downloader(session)
    downloader.download(url, file_path, file_name, forced, thread_num)


class DownloaderProcessPool(object):

    def __init__(self, pool_size=5) -> None:
        super().__init__()
        if 0 >= pool_size:
            pool_size = 5
        self._pool = Pool(pool_size)

    def wait(self):
        self._pool.close()
        self._pool.join()

    def add_task(self, url, file_path='', file_name='', session=None, forced=False, thread_num=5):
        self._pool.apply_async(func=_download_process, args=(url, file_path, file_name, session, forced, thread_num,))
